import request from '@/utils/request'
import type {
  HasSpuResponseData,
  AllTradeMark,
  SpuHasImg,
  HasSaleAttrResponseData,
  AllSaleAttrResponseData,
  SpuData,
  SkuData,
  SkuInfoData
} from './type'

enum API {
  //获取已有的SPU的数据
  HASSPU_URL = '/admin/product/',
  //获取全部SPU的品牌数据
  ALLTRADEMARK_URL = '/admin/product/baseTrademark/getTrademarkList',
  //获取某一个Spu下的全部售卖商品的图片
  IMAGE_URL = '/admin/product/spuImageList/',
  //某一个已有的SPU拥有的销售属性
  SPUHASSALEATTR_URL = '/admin/product/spuSaleAttrList/',
  //获取平台中全部的销售属性 (整个平台销售属性一共最多三个)
  ALLSALEATTR_URL = '/admin/product/baseSaleAttrList',
  //追加新的spu
  ADDSPU_URL = '/admin/product/saveSpuInfo',
  //更新已有的spu
  UPDATESPU_URL = '/admin/product/updateSpuInfo',
  //给某一个已有的spu添加sku
  ADDSKU_URL = '/admin/product/saveSkuInfo',
  //查看某一个已有的spu下的全部sku
  SKUINFO_URL = '/admin/product/findBySpuId/',
  //删除已有的spu
  REMOVESPU_URL = '/admin/product/deleteSpu/'
}

// 获取已有的SPU列表数据的接口
export const reqHasSpu = (page: number, limit: number, category3Id: number | string) =>
  request.get<any, HasSpuResponseData>(
    API.HASSPU_URL + `${page}/${limit}?category3Id=${category3Id}`
  )

//获取全部SPU的品牌数据
export const reqAllTradeMark = () => request.get<any, AllTradeMark>(API.ALLTRADEMARK_URL)

//获取某一个Spu下的全部售卖商品的图片
export const reqSpuImageList = (spuId: number) => request.get<any, SpuHasImg>(API.IMAGE_URL + spuId)

//获取某一个已有的SPU拥有的销售属性
export const reqSpuHasSaleAttr = (spuId: number) =>
  request.get<any, HasSaleAttrResponseData>(API.SPUHASSALEATTR_URL + spuId)

//获取平台中全部的销售属性 (整个平台销售属性一共最多三个)
export const reqAllSaleAttr = () => request.get<any, AllSaleAttrResponseData>(API.ALLSALEATTR_URL)

//添加/更新一个新的SPU的接口(data为新增或已有的spu对象)
export const reqAddOrUpdateSpu = (data: SpuData) => {
  if (data.id) {
    return request.post<any, any>(API.UPDATESPU_URL, data)
  } else {
    return request.post<any, any>(API.ADDSPU_URL, data)
  }
}

//给某一个已有的spu添加sku
export const reqAddSku = (data: SkuData) => request.post<any, any>(API.ADDSKU_URL, data)

//查看某一个已有的spu下的全部sku
export const reqSkuList = (spuId: number | string) =>
  request.get<any, SkuInfoData>(API.SKUINFO_URL + spuId)

//删除已有的spu
export const reqRemoveSpu = (spuId: number | string) =>
  request.delete<any, any>(API.REMOVESPU_URL + spuId)
